<?php

class Qpcr_IndexController extends Zend_Controller_Action
{ 
    protected $_vsession;
    protected $_upload;
    
    public function init()
    {
       $this->__session=Zend_Registry::get('session');
       $this->_upload = new Files_Model_UploadModel();
    }

    public function indexAction()
    {
        echo "<br>en index controllers del qpcr ";//die();
        
        $objForm = new Qpcr_Form_UploadForm();
        $this->view->form = $objForm;       
    }

    public function uploadAction()
    {
        
        echo "modulo:qpcr/controlador:index/accion:upload";


       
        //uso del require cuando son librerias externas al zend
        require_once(APPLICATION_PATH . "/../library/Upload.php");
        require_once(APPLICATION_PATH . "/../library/fileReader.php");
        require_once(APPLICATION_PATH . "/../library/pclzip/pclzip.lib.php");
        //APPLICATION_PATH "/../library
        
        //instancia objeto upload
        $objLoad =new Upload();
        //$objLoad = new Mi_Upload();

       
        //instancia formulario        
        $objFileReader = new fileReader();
        //$objFileReader = new Mi_fileReader();

       
        
        $url = explode(DIRECTORY_SEPARATOR, trim(dirname(__DIR__)));
        $ruta_guardar=$url[0].DIRECTORY_SEPARATOR.$url[1].DIRECTORY_SEPARATOR.'zf_unZip';

        $ruta_final_guardar=$url[0].DIRECTORY_SEPARATOR.$url[1].DIRECTORY_SEPARATOR.'zf_qpcr'.DIRECTORY_SEPARATOR;
        $_SESSION['ruta_guardar']=$ruta_guardar;
        $objForm = new Qpcr_Form_UploadForm();

//        echo "<br>".$ruta_final_guardar;
//        echo $ruta_guardar;
//        die();

        $params = $this->_request->getParams();
        //fecha momenbto subida
        $dateup = date('YmdHis');

        $vsesion = $this->__session->rg_session;

        if( $this->_request->isPost() && $objForm->isValid($params) ){
                //lugar a guardar el file
                $location = $objForm->fileupload->getFileName();
                $separator = DIRECTORY_SEPARATOR;
                $url =  explode($separator, $location);
                $directorio=$url[0].$separator.$url[1];

                $namefile= $objForm->getValues();
                $archive = new PclZip($location);

                $objLoad->setRouteFolder($ruta_guardar);
                $tmp_file='tmp_qpcr_'.$dateup;
                $rute_create=$objLoad->createFolder($tmp_file);
                $objLoad->setRouteFolder($rute_create);
                $ruta_extraer=$ruta_guardar.$separator.$tmp_file;

                $list= $archive->extract(PCLZIP_OPT_PATH,$ruta_extraer,PCLZIP_OPT_REMOVE_ALL_PATH);
                $filesindirectory = $objLoad->getFileListInFolder($objLoad->getFolderUpload());
                //cantidad de archiuvos descomprimidos
                $j = count($filesindirectory);
                
                for($i=0;$j>$i;$i++){
                $NuevoNombre = md5($filesindirectory[$i]['name']).$filesindirectory[$i]['extension'];
                $Archivo_Mover= $objLoad->getFolderUpload();
                $ArchivoExtension=$filesindirectory[$i]['extension'];
                //obtener contenido del archivo
                $Contenido = $objFileReader->read($Archivo_Mover, $ArchivoExtension);

                if($objLoad->moveFile($objLoad->getFolderUpload().$separator.$filesindirectory[$i]['archivo'],$ruta_final_guardar,$NuevoNombre));
                {
                   $data['id_upload'] ='';
                    $data['id_session'] =$vsesion;
                    $data['file_type'] = 1;
                    $data['file_name_encrypt'] = $NuevoNombre;
                    $data['file_url'] = $ruta_final_guardar;
                    //die();
                    $this->_upload->insert($data);
                    //$this->_upload->($data);
                }

                }
            //$this->_redirect('/qpcr/index/generarxml');
            //$this->_redirect('/proteomics/upload/index');
            $this->_redirect('/proteomics/upload/');

            
            }

$this->view->form = $objForm;


    }

    public function generarxmlAction()
    {
       
    echo "generar xml";
    $registroSubida = $this->_upload->obtenerSubida(1);

//    for($i=0;$i<= count($registroSubida);$i++)
//    {
//
//    }
    var_dump($registroSubida);
    die();

    }


}

